/*--------------------权限模块.角色管理--ly-----------------*/
/*--------------------（管理员权限）角色修改功能 role_edit.html------------------*/

$(document).ready(function() {

	//获取cookie:userid & Wtid
	var roleid = $.cookie("roleid");
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	//填充数据
	var searchurl = jurisdictionport + '/RoleController/selectByPrimaryKey'; //查询
	$.get(searchurl, {
		'roleid': roleid
	}, function(result) {
		var code = result.code;
		var editlist = result.object;
		if(code == 0) {
			$("#L_rolename").val(editlist.rolename); //角色名
			$("#L_description").val(editlist.description); //手机号码
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});

	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			rolename: function(value) {
				if(value.length < 3) {
					return '昵称至少得3个字符啊';
				}
			},
		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var rolename = $("#L_rolename").val(); //用户名
			var description = $("#L_description").val(); //邮箱
			console.log(rolename,description);
			//监听提交
			form.on('submit(edit)', function() {
				//递交修改
				editData(rolename,description);
				return false;
			});
		});

		//修改用户信息
		var editurl = jurisdictionport + '/RoleController/modifyByRoleid'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(rolename,description) {
			//修改 edit
			$.get(editurl, {
				roleid: roleid,
				rolename: rolename,
				description:description,
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
//						parent.layer.close(index);
					});
				}
			})
		}

	});
})